import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter);

const routes = [
    {
        path: '/login',
		name: 'login',
        component: () => import('../views/LoginView.vue'),
        meta: { requiresAuth: false } // 不需要身份验证
    },
    {
        path: '/register',
        component: () => import('../views/RegisterView.vue'),
        meta: { requiresAuth: false } // 不需要身份验证
    },
    {
        path: '/',
        name: 'main',
        component: () => import('../views/Main.vue'),
        meta: { requiresAuth: true }, // 需要身份验证
        children: [
			{
			    path: '/home',
			    component: () => import('../views/home.vue'),
                meta: { requiresAuth: true }, // 需要身份验证
			},
			{
				path: 'schedule',
				component: () => import('../views/schedule/ScheduleView.vue'),
				meta: { requiresAuth: true } // 需要身份验证
            },
            {
                path: 'schedule/add',
                component: () => import('../views/schedule/AddScheduleView.vue'),
                meta: { requiresAuth: true } // 需要身份验证
            },
            {
                path: 'files',
                component: () => import('../views/file/FileView.vue'),
                meta: { requiresAuth: true } // 需要身份验证
            },
            {
                path: 'image',
                name: 'image',
                component: () => import('../views/image/ImageCompress.vue'),
                meta: { requiresAuth: true } // 需要身份验证
            },
            {
                path: 'audio',
                name: 'audio',
                component: () => import('../views/VideoToAudioConverter/VideoToAudioConverter.vue'),
                meta: { requiresAuth: true } // 需要身份验证
            },
            {
                path: '/note/add',
                component: () => import('../views/note/AddNote.vue'),
                meta: { requiresAuth: true }, // 需要身份验证
            },
            {
                path: '/note/list',
                component: () => import('../views/note/NoteList.vue'),
                meta: { requiresAuth: true }, // 需要身份验证
            },
            {
                path: '/note/show',
                component: () => import('../views/note/ShowNote.vue'),
                meta: { requiresAuth: true }, // 需要身份验证
            },
            {
                path: '/note/edit',
                component: () => import('../views/note/EditNote.vue'),
                meta: { requiresAuth: true }, // 需要身份验证
            },
            {
                path: '/admin',
                component: () => import('../views/AdminView.vue'),
                meta: { requiresAuth: true }, // 需要身份验证
            },

        ]
    },


];

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
});

router.beforeEach((to, from, next) => {
    const requiresAuth = to.meta.requiresAuth; // 获取路由配置中的 requiresAuth 字段

    // 判断用户是否已登录，存储在localStorage 中的登录状态
    const isLoggedIn = localStorage.getItem("user") != null;

    if (requiresAuth && !isLoggedIn) {
        // 如果请求路径需要身份验证且用户未登录，则重定向到登录页面
        next('/login');
    } else {
        // 否则，允许用户继续访问
        next();
    }
});

//解决Avoided redundant navigation to current location报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
};

export default router;
